package ysq.xyg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.DbShare;
import ysq.xyg.model.info.DbShareInfo;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.DbShareService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;

/**
 * @author yu sheng qiang
 * @create 2017-04-19 14:15
 **/
@Controller
@RequestMapping("/dbShare")
public class DbShareController extends BaseController {
    private static Logger log= LoggerFactory.getLogger(DbShareController.class);
    @Resource
    private DbShareService dbShareService;

    private DbShare dbShare;

    @RequestMapping("/listPage")
    public ModelAndView List(HttpServletRequest request, HttpServletResponse response){
        return new ModelAndView("/dbShareList");
    }

    @RequestMapping(value = "/JsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject JsonList(DbShare dbShare, Page page) throws MalformedURLException, UnsupportedEncodingException {
        PageInfo<DbShare> pageInfo=dbShareService.pageInfo(dbShare,page.getLimit(),page.getPage());
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","");
        jsonObject.put("count",pageInfo.getTotal());
        if(pageInfo !=null && pageInfo.getList() !=null && pageInfo.getList().size() > 0){
            JSONObject row = null;
            JSONArray rows = new JSONArray();
            for(DbShare item:pageInfo.getList()){
                row=new JSONObject();
                row.put("id",item.getId());
                row.put("shareTitle",item.getShareTitle());
                row.put("shareDesc",item.getShareDesc());
                row.put("sharePic",item.getSharePic());
                row.put("createTime", DateUtil.DateToStr(item.getCreateTime(), DateUtil.DATE_TIME_PATTERN));
                rows.add(row);
            }
            jsonObject.put("data",rows);
        }
        return jsonObject;
    }

    @RequestMapping(value = "/SelectEntity",method = RequestMethod.GET)
    public ModelAndView SelectEntity(String id){
        ModelAndView view=new ModelAndView("/dbShareUpdateOrSave");
        DbShare dbShare=dbShareService.selectByPrimaryKey(id);
        view.addObject("dbShare",dbShare);
        return view;
    }

    @RequestMapping(value = "/UpdateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject UpdateOrAddEntity(DbShareInfo dbShareInfo){
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(dbShareInfo.getId()==null || "".equals(dbShareInfo.getId())){
            dbShare=new DbShare();
            dbShare.setId(BaseUtil.uId());
            dbShare.setCreateTime(new Date());
            dbShare.setShareDesc(dbShareInfo.getShareDesc());
            dbShare.setSharePic(dbShareInfo.getSharePic());
            dbShare.setShareTitle(dbShareInfo.getShareTitle());
            dbShare.setContent(dbShareInfo.getContent());
            int id=dbShareService.insert(dbShare);
            if(id <= 0){
                returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);
            }
        }else{
            dbShare=dbShareService.selectByPrimaryKey(dbShareInfo.getId());
            dbShare.setCreateTime(new Date());
            dbShare.setShareDesc(dbShareInfo.getShareDesc());
            dbShare.setSharePic(dbShareInfo.getSharePic());
            dbShare.setShareTitle(dbShareInfo.getShareTitle());
            dbShare.setContent(dbShareInfo.getContent());
            int id=dbShareService.updateByPrimaryKey(dbShare);
            if(id <= 0){
                returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
    @RequestMapping(value = "/DeleteEntity",method = RequestMethod.GET)
    public @ResponseBody JSONObject DeleteEntity(String id){
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(StringUtil.isEmpty(id)){
            returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_CANSHU);
        }else{
            int num=dbShareService.deleteByPrimaryKey(id);
            if(num <= 0){
                returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_DELETE);
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
}
